package com.wsm.common.controller;

import com.wsm.common.annotation.DuplicateSubmitToken;
import com.wsm.common.annotation.OperateLog;
import com.wsm.common.business.BlackipBusiness;
import com.wsm.common.domain.BlackipDO;
import com.wsm.common.enums.BaseEnum;
import com.wsm.common.utils.PageUtils;
import com.wsm.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 访问黑名单
 *
 * @author wangshimin
 * @email wangshiminhome@foxmail.com
 * @date 2020-02-17 17:03:32
 * @description : 访问黑名单
 */
@Api(tags = "访问黑名单")
@Controller
@RequestMapping("/common/blackip")
public class BlackipController {
	@Autowired
	private BlackipBusiness blackipBusiness;

	@GetMapping()
	@ApiOperation(value = "跳转展示界面")
	@RequiresPermissions("common:blackip:look")
	String Blackip() {
		return "common/blackip/blackip";
	}

	@ResponseBody
	@GetMapping("/list")
	@ApiOperation(value = "获取集合列表")
	@RequiresPermissions("common:blackip:look")
	@DuplicateSubmitToken(save = true, expire = 2, time = TimeUnit.MINUTES)
	public Result<PageUtils> list(@RequestParam Map<String, Object> params) {
		params.put("delFlag" , BaseEnum.DEL_FLAF.Not_del.getCode());
		return blackipBusiness.list(params);
	}

	@GetMapping("/add")
	@ApiOperation(value = "跳转增加界面")
	@RequiresPermissions("common:blackip:add")
	String add() {
		return "common/blackip/add";
	}

	@GetMapping("/edit/{id}")
	@ApiOperation(value = "跳转修改界面")
	@RequiresPermissions("common:blackip:edit")
	String edit(@PathVariable("id") Long id, Model model) {
		BlackipDO blackip = blackipBusiness.get(id);
		model.addAttribute("blackip", blackip);
		return "common/blackip/edit";
	}

	/**
	 * 保存
	 */
	@OperateLog("增加访问黑名单")
	@ResponseBody
	@PostMapping("/save")
	@ApiOperation(value = "增加")
	@DuplicateSubmitToken(save = true, expire = 2, time = TimeUnit.MINUTES)
	@RequiresPermissions("common:blackip:add")
	public Result save(BlackipDO blackip) {
		return blackipBusiness.save(blackip);
	}

	/**
	 * 修改
	 */
	@OperateLog("修改访问黑名单")
	@ResponseBody
	@RequestMapping("/update")
	@ApiOperation(value = "修改")
	@DuplicateSubmitToken(save = true, expire = 2, time = TimeUnit.MINUTES)
	@RequiresPermissions("common:blackip:edit")
	public Result update(BlackipDO blackip) {
		return blackipBusiness.update(blackip);
	}

	/**
	 * 删除
	 */
	@OperateLog("删除访问黑名单")
	@ResponseBody
	@PostMapping("/remove")
	@ApiOperation(value = "删除")
	@DuplicateSubmitToken(save = true, expire = 2, time = TimeUnit.MINUTES)
	@RequiresPermissions("common:blackip:remove")
	public Result remove(Long id) {
		return blackipBusiness.remove(id);
	}

	/**
	 * 删除
	 */
	@OperateLog("批量删除访问黑名单")
	@ResponseBody
	@PostMapping("/batchRemove")
	@ApiOperation(value = "批量删除")
	@DuplicateSubmitToken(save = true, expire = 2, time = TimeUnit.MINUTES)
	@RequiresPermissions("common:blackip:batchRemove")
	public Result remove(@RequestParam("ids[]") Long[] ids) {
		return blackipBusiness.batchRemove(ids);
	}

	/**
	 * 删除
	 */
	@OperateLog("伪删除访问黑名单")
	@ResponseBody
	@PostMapping("/removeDel")
	@ApiOperation(value = "伪删除")
	@DuplicateSubmitToken(save = true, expire = 2, time = TimeUnit.MINUTES)
	@RequiresPermissions("common:blackip:removeDel")
	public Result removeDel(Long id) {
		return blackipBusiness.removeOrDel(id);
	}

	/**
	 * 删除
	 */
	@OperateLog("批量伪删除访问黑名单")
	@ResponseBody
	@PostMapping("/batchRemoveDel")
	@ApiOperation(value = "批量伪删除")
	@DuplicateSubmitToken(save = true, expire = 2, time = TimeUnit.MINUTES)
	@RequiresPermissions("common:blackip:batchRemoveDel")
	public Result batchRemoveDel(Long[] ids) {
		return blackipBusiness.batchRemoveOrDel(ids);
	}

	/**
	 * 修改可用状态
	 */
	@OperateLog("修改可用状态访问黑名单")
	@ResponseBody
	@PostMapping("/changeStatus")
	@ApiOperation(value = "修改可用状态")
	@RequiresPermissions("common:blackip:change")
	@DuplicateSubmitToken(save = true, expire = 2, time = TimeUnit.MINUTES)
	public Result changeUseFlag(Long id, Integer useFlag) {
		return blackipBusiness.updateUseFlag(id, useFlag);
	}

}
